<template>
     <div>{{courseTimerStatus}}</div>
</template>

<script>
import moment from 'moment';

export default {
  name: 'times',
  props: {
    dateStart: {
      type: String,
      default: '',
    },
    dateEnd: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      timeShow: false,
      course: {
        courseNo: '',
        courseName: '',
        teacherName: '',
        courseDesciption: '',
        coursePeriod: '',
        startTime: '',
        endTime: '',
        courcePrice: 0,
        stockQuantity: 0,
        courseType: 0,
        coursePic: '',
      },
      courseTimerStatus: '距离活动开始时间 00:00:00',
      timer: null,
      isTimerStop: true,
      showButtonType: 0,
      isBookButtonDisabled: false,
      load: false,
    };
  },
  created() {
    this.timeShow = false;
  },
  mounted() {
    this.countdown();
  },
  methods: {
    countdown() {
      const date = new Date();
      const now = date.getTime();
      // 设置截止时间
      const time = moment(this.dateEnd).format('YYYY/MM/DD HH:mm:ss');
      const endDate = new Date(time);
      const end = endDate.getTime();
      // 时间差
      const leftTime = end - now;
      let times;
      if (leftTime >= 0) {
        times = setInterval(this.countdown, 1000);
        const day = Math.floor(leftTime / (24 * 3600 * 1000));
        // Math.floor(leave1/(3600*1000))
        const leave1 = leftTime % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
        const hr = Math.floor(leave1 / (3600 * 1000));
        const leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
        const min = Math.floor(leave2 / (60 * 1000));
        // 计算相差秒数
        const leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
        const sec = Math.round(leave3 / 1000);
        this.day = day > 9 ? day : `0${day}`;
        this.hr = hr > 9 ? hr : `0${hr}`;
        this.min = min > 9 ? min : `0${min}`;
        this.sec = sec > 9 ? sec : `0${sec}`;
        this.courseTimerStatus = `距离活动结束 ${this.day}日${this.hr}时${this.min}分${this.sec}秒`;
      } else {
        clearInterval(times);
      }
      // 递归每秒调用countTime方法，显示动态时间效果
    },
  },
};
</script>
